package com.inventory.service.product;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.inventory.persistence.ProductType;
import com.inventory.persistence.ProductTypeExample;
import com.inventory.persistence.dao.ProductTypeMapper;

public class ProductTypeServiceImpl implements ProductTypeService {
	@Autowired
	private ProductTypeMapper productTypeMapper;

	@Override
	public List<ProductType> getAllProductTypes() {
		ProductTypeExample productTypeExample = new ProductTypeExample();
		productTypeExample.createCriteria().andOidGreaterThan((long) 0);
		return productTypeMapper.selectByExample(productTypeExample);
	}

	@Override
	public void batchUpdateProductTypes(ProductType[] productTypes) {
		for (ProductType productType : productTypes) {
			if (productType.getCategory() != null && productType.getCategory().length() > 0) {
				ProductTypeExample example = new ProductTypeExample();
				example.createCriteria().andOidEqualTo(productType.getOid());
				productTypeMapper.updateByExample(productType, example);
			}
		}
	}

	@Override
	public void batchInsertProductTypes(ProductType[] productTypes) {
		for (ProductType productType : productTypes) {
			if (productType.getCategory() != null && productType.getCategory().length() > 0) {
				productType.setVersionStamp(1);
				productTypeMapper.insert(productType);
			}
		}
	}

	@Override
	public void deleteProductTypes(long oid) {
		productTypeMapper.deleteByPrimaryKey(oid);
	}

	public void setProductTypeMapper(ProductTypeMapper productTypeMapper) {
		this.productTypeMapper = productTypeMapper;
	}

}
